﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BotonJXC.BLL;
using BotonJXC.DAO;
using BotonJXC.Entity;
using BotonJXC.Forms.Events;
using NHibernate;

namespace BotonJXC.Forms.Controls
{
    public partial class ProductUnitControl : BaseInfoControl
    {
        UnitBLL _unitBll = new UnitBLL();
        public ProductUnitControl()
        {
            InitializeComponent();
        }

        private void ProductUnitControl_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            PageModel<Unit> pageModel = _unitBll.Find(ktbUnitName.Text.Trim(), pager1.PageStart, pager1.PageSize);
            pager1.TotalCount = pageModel.TotalCount;
            kdgvUnit.DataSource = new BindingList<Unit>(pageModel.Results);
        }

        public override IList<object> SelectedItems
        {
            get
            {
                IList<object> selectedItems = new List<object>();
                foreach (DataGridViewRow r in kdgvUnit.SelectedRows)
                {
                    selectedItems.Add(r.DataBoundItem);
                }
                return selectedItems;
            }
        }

        public override void SaveInfo()
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvUnit.Rows)
                {
                    Unit u = r.DataBoundItem as Unit;
                    if (u == null)
                        continue;
                    if (u.UnitId == 0)
                    {
                        _unitBll.Save(u);
                    }
                    else if ("1".Equals(r.Tag) && u.UnitId != 0)
                    {
                        _unitBll.Update(u);
                    }
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }

            LoadData();
        }

        public override void AddInfo()
        {
            IList<Unit> units = (IList<Unit>)kdgvUnit.DataSource;
            Unit u = new Unit();
            u.UnitName = "";
            units.Add(u);
            kdgvUnit.Refresh();
        }

        public override void DeleteInfo()
        {
            IList<Unit> units = (IList<Unit>)kdgvUnit.DataSource;
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvUnit.SelectedRows)
                {
                    Unit unit = r.DataBoundItem as Unit;
                    if (unit != null)
                    {
                        _unitBll.Delete(unit.UnitId);
                    }
                    units.RemoveAt(r.Index);
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
            kdgvUnit.Refresh();
        }

        private void kdgvUnit_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            kdgvUnit.Rows[e.RowIndex].Tag = "1";
        }

        private void kbQuery_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void kdgvUnit_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > 0 && e.ColumnIndex < 1)
            {
                
            }
        }

        private void pager1_PageChanged(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
